FROM alpine:3.19

# Environments
ENV TIMEZONE=Asia/Shanghai
ENV PHP_MEMORY_LIMIT=512M
ENV MAX_UPLOAD=10M
ENV PHP_MAX_FILE_UPLOAD=8
ENV PHP_MAX_POST=100M
ENV COMPOSER_ALLOW_SUPERUSER=1

#update install source
RUN echo "http://dl-cdn.alpinelinux.org/alpine/v3.19/community" >> /etc/apk/repositories
# RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories
# RUN set -eux && sed -i 's/dl-cdn.alpinelinux.org/mirrors.ustc.edu.cn/g' /etc/apk/repositories

#2.ADD-PHP-FPM

# Mirror mirror switch to Alpine Linux - http://dl-4.alpinelinux.org/alpine/
RUN apk update \
	&& apk upgrade \
	&& apk add \
		curl \
		tzdata \
		php82-fpm\
	    php82 \
	    php82-dev \
	    php82-pecl-apcu \
	    php82-bcmath \
	    php82-xmlwriter \
	    php82-ctype \
	    php82-curl \
	    php82-exif \
	    php82-iconv \
	    php82-intl \
	    php82-json \
	    php82-mbstring\
	    php82-opcache \
	    php82-openssl \
	    php82-pcntl \
	    php82-pdo \
	    php82-mysqlnd \
	    php82-mysqli \
	    php82-pdo_mysql \
	    php82-pdo_pgsql \
	    php82-phar \
	    php82-posix \
	    php82-session \
	    php82-xml \
	    php82-simplexml \
	    libmcrypt \
	    php82-openssl \
	    php82-xsl \
	    php82-zip \
	    php82-zlib \
	    php82-dom \
	    php82-pecl-redis \
	    php82-tokenizer \
	    php82-gd \
		#php82-pecl-mongodb \
		php82-fileinfo \
		php82-xmlreader \
 	&& cp /usr/share/zoneinfo/${TIMEZONE} /etc/localtime \
	&& echo "${TIMEZONE}" > /etc/timezone \
	&& apk del tzdata \
 	&& rm -rf /var/cache/apk/*

# https://github.com/docker-library/php/issues/240
# https://gist.github.com/guillemcanal/be3db96d3caa315b4e2b8259cab7d07e
# https://forum.alpinelinux.org/forum/installation/php-iconv-issue

RUN mkdir -p /usr/local/var/log/php82/
RUN mkdir -p /usr/local/var/run/
COPY ./docker/php-fpm.conf /etc/php82/
COPY ./docker/www.conf /etc/php82/php-fpm.d/

#RUN apk add --no-cache --repository http://dl-3.alpinelinux.org/alpine/edge/testing gnu-libiconv
#ENV LD_PRELOAD /usr/lib/preloadable_libiconv.so php
#RUN rm -rf /var/cache/apk/*

# Set environments
RUN sed -i "s|;*date.timezone =.*|date.timezone = ${TIMEZONE}|i" /etc/php82/php.ini && \
	sed -i "s|;*memory_limit =.*|memory_limit = ${PHP_MEMORY_LIMIT}|i" /etc/php82/php.ini && \
	sed -i "s|;*upload_max_filesize =.*|upload_max_filesize = ${MAX_UPLOAD}|i" /etc/php82/php.ini && \
	sed -i "s|;*max_file_uploads =.*|max_file_uploads = ${PHP_MAX_FILE_UPLOAD}|i" /etc/php82/php.ini && \
	sed -i "s|;*post_max_size =.*|post_max_size = ${PHP_MAX_POST}|i" /etc/php82/php.ini && \
	sed -i "s|;*cgi.fix_pathinfo=.*|cgi.fix_pathinfo= 0|i" /etc/php82/php.ini && \
	sed -i "s|;*expose_php =.*|expose_php = Off|i" /etc/php82/php.ini

#3.Install-Composer
RUN curl -sS https://getcomposer.org/installer | \
    php -- --install-dir=/usr/bin/ --filename=composer

#4.ADD-NGINX
RUN apk add nginx
COPY ./docker/nginx/conf.d/default.conf /etc/nginx/conf.d/
COPY ./docker/nginx/nginx.conf /etc/nginx/

RUN mkdir -p /usr/share/nginx/html/public/
RUN mkdir -p /var/log/nginx
RUN mkdir -p /var/lib/nginx/logs

WORKDIR /usr/share/nginx/html/public/
COPY . .

RUN composer install && composer dump-autoload

#5.ADD-SUPERVISOR
RUN apk add supervisor \
 && mkdir -p /var/log/supervisor \
 && mkdir -p /etc/supervisor/conf.d \
 && chmod 777 /var/log/supervisor \
 && rm -rf /var/cache/apk/*

COPY ./docker/supervisor/conf.d/ /etc/supervisor/conf.d/

#6.ADD-CRONTABS
COPY ./docker/crontabs/default /var/spool/cron/crontabs/
RUN cat /var/spool/cron/crontabs/default >> /var/spool/cron/crontabs/root
RUN mkdir -p /var/log/cron \
 && touch /var/log/cron/cron.log

#7.ADD-REDIS
RUN apk add redis

#9.添加启动脚本
COPY ./entrypoint.sh /
RUN chmod +x /entrypoint.sh

# 可选
#CMD ["supervisord", "--nodaemon", "--configuration", "/etc/supervisor/conf.d/supervisord.conf"]
#CMD ["supervisord","-n","-c","/etc/supervisor/conf.d/supervisord.conf"]

ENTRYPOINT ["sh","-C","./entrypoint.sh"]

# 暴露端口
EXPOSE 80
